<?php
/**
 * @package YARBB
 * @copyright Авторские права (C) 2009 www.yar-it.com. Все права защищены.
 * @license Лицензия http://www.gnu.org/licenses/gpl-2.0.htm GNU/GPL, или help/license.php
 * YARBB! - свободное программное обеспечение распространяемое по условиям лицензии GNU/GPL
 * Для получения информации о используемых расширениях и замечаний об авторском праве, смотрите файл copyright.php.
 */
// don't allow direct linking
defined( '_VALID_MOS' ) or die();

if (isset($_GET['action']))
	define('YARBB_QUIET_VISIT', 1);

$action = isset($_GET['action']) ? $_GET['action'] : null;
if ($action == 'rules') {
	echo $mainframe->setPageTitle( _Y_RULES.' - '.yarbb_htmlspecialchars($yarbb_config['o_board_title']));
	?>

<div class="block">
	<h2><span><?php echo _Y_RULES; ?></span></h2>
	<div class="box">
		<div class="inbox">
			<p><?php echo $yarbb_config['o_rules_message'] ?></p>
		</div>
	</div>
</div>
	<?php
	require $yarbb_path.'/task/footer.php';
} else if ($action == 'markread') {
	if ($email_setting == 2 && ($yarbb_user['g_id'] > YARBB_MOD && !$yarbb_user['g_global_moderation']))
		mosRedirect(sefRelToAbs(YARBB_URL), _NOT_AUTH);

	$database->setQuery('UPDATE #__forum_users SET last_visit='.$yarbb_user['logged'].' WHERE id='.$yarbb_user['id']);
	$database->query();

	mosRedirect(sefRelToAbs(YARBB_URL), _Y_MISALLMARK);
} else if (isset($_GET['email'])) {
	if ($yarbb_user['is_guest'])
		mosRedirect(sefRelToAbs(YARBB_URL), _NOT_AUTH);
	
	$recipient_id = intval($_GET['email']);

	if ($recipient_id < 2)
		mosRedirect(sefRelToAbs(YARBB_URL), _Y_404);
	//s
	$database->setQuery('SELECT username, email, email_setting FROM #__forum_users WHERE id='.$recipient_id);
	$database->query();

	if (!$database->getNumRows())
		mosRedirect(sefRelToAbs(YARBB_URL), _Y_404);
	list($recipient, $recipient_email, $email_setting) = $database->loadRow();

	if ($email_setting == 2 && $yarbb_user['g_id'] > YARBB_MOD)
		mosRedirect(sefRelToAbs(YARBB_URL), _Y_FORMEMALOFF);

	if (isset($_POST['form_sent'])) {
		// Clean up message and subject from POST
		$subject = yarbb_trim($_POST['req_subject']);
		$message = yarbb_trim($_POST['req_message']);
		if ($subject == '')
			mosRedirect(sefRelToAbs(YARBB_URL), _PLEASE_ENTER_SUBJECT);
		else if ($message == '')
			mosRedirect(sefRelToAbs(YARBB_URL), _PLEASE_ENTER_MESSAGE);
		else if (strlen($message) > 65535)
			mosRedirect(sefRelToAbs(YARBB_URL),_Y_YLONGPOSTS);

		// Загружаем шаблон
		$mail_tpl = trim(file_get_contents($yarbb_path.'/inc/mail_templates/'.$mosConfig_lang.'/form_email.tpl'));
		// The first row contains the subject
		$first_crlf = strpos($mail_tpl, "\n");
		$mail_subject = trim(substr($mail_tpl, 8, $first_crlf-8));
		$mail_message = trim(substr($mail_tpl, $first_crlf));
		$mail_subject = str_replace('<mail_subject>', $subject, $mail_subject);
		$mail_message = str_replace('<sender>', $yarbb_user['username'], $mail_message);
		$mail_message = str_replace('<board_title>', $yarbb_config['o_board_title'], $mail_message);
		$mail_message = str_replace('<mail_message>', $message, $mail_message);
		$mail_message = str_replace('<board_mailer>', $yarbb_config['o_board_title'].' '._Y_YARROBOTMAIL, $mail_message);
		require_once $yarbb_admin_path.'/include/email.php';
		yarbb_mail($recipient_email, $mail_subject, $mail_message, '"'.str_replace('"', '', $yarbb_user['username']).'" <'.$yarbb_user['email'].'>');
		mosRedirect(htmlspecialchars($_POST['redirect_url']), _THANK_MESSAGE);
	}
	// Try to determine if the data in HTTP_REFERER is valid (if not, we redirect to the users profile after the e-mail is sent}
	$redirect_url = sefRelToAbs(YARBB_URL); //(isset($_SERVER['HTTP_REFERER']) && preg_match('#^'.preg_quote($yarbb_path).'/(.*?)\.php#i', $_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : 'yarbb.php';

	$required_fields = array('req_subject' => _SUBJECT_PROMPT, 'req_message' => _Y_MESSAGE);
	$focus_element = array('email', 'req_subject');
	?>
<div class="blockform">
	<h2><span><?php echo _EMAIL_DESCRIPTION; ?> <?php echo yarbb_htmlspecialchars($recipient) ?></span></h2>
	<div class="box">
		<form id="email" method="post" action="<?php echo sefRelToAbs(YARBB_URL.'&amp;task=misc&amp;email='.$recipient_id); ?>" onsubmit="this.submit.disabled=true;if(process_form(this)){return true;}else{this.submit.disabled=false;return false;}">
			<div class="inform">
				<fieldset>
					<legend><?php echo _CONTACT_FORM_NC; ?></legend>
					<div class="infldset txtarea">
						<input type="hidden" name="form_sent" value="1" />
						<input type="hidden" name="redirect_url" value="<?php echo $redirect_url ?>" />
						<label><strong><?php echo _SUBJECT_PROMPT; ?></strong><br />
							<input class="longinput" type="text" name="req_subject" size="75" maxlength="70" tabindex="1" />
							<br />
						</label>
						<br />
						<label><strong><?php echo _Y_MESSAGE; ?>:</strong><br />
							<textarea name="req_message" rows="10" cols="75" tabindex="2"></textarea>
						</label>
						<p><?php echo _Y_EMAILNOTE; ?></p>
					</div>
				</fieldset>
			</div>
			<input type="submit" name="submit" value="<?php echo _SUBMIT_BUTTON; ?>" tabindex="3" accesskey="s" />
			<input name="" type="button" onclick="javascript:history.go(-1)" value="<?php echo _BUTTON_CANCEL; ?>" />
		</form>
	</div>
</div>
	<?php
	require $yarbb_path.'/task/footer.php';

} else if (isset($_GET['subscribe'])) {
	if ($yarbb_user['is_guest'] || $yarbb_config['o_subscriptions'] != '1')
		mosRedirect(sefRelToAbs(YARBB_URL), _NOT_AUTH);

	$topic_id = intval($_GET['subscribe']);
	if ($topic_id < 1)
		mosRedirect(sefRelToAbs(YARBB_URL), _Y_404);

	$database->setQuery('SELECT 1 FROM #__forum_subscriptions
	WHERE user_id='.$yarbb_user['id'].' 
	AND topic_id='.$topic_id);
	$database->query();
	
	if ($database->getNumRows())
		mosRedirect(sefRelToAbs(YARBB_URL.'&amp;task=viewtopic&amp;id='.$topic_id), _Y_SUBSKADD1);

	$database->setQuery('INSERT INTO #__forum_subscriptions (user_id, topic_id) VALUES('.$yarbb_user['id'].' ,'.$topic_id.')');
	$database->query();

	mosRedirect(sefRelToAbs(YARBB_URL.'&amp;task=viewtopic&amp;id='.$topic_id), _Y_SUBSKADD);
} else if (isset($_GET['unsubscribe'])) {
	if ($yarbb_user['is_guest'] || $yarbb_config['o_subscriptions'] != '1')
		mosRedirect(sefRelToAbs(YARBB_URL), _NOT_AUTH);

	$topic_id = intval($_GET['unsubscribe']);
	if ($topic_id < 1)
		mosRedirect(sefRelToAbs(YARBB_URL), _Y_404);

	$database->setQuery('SELECT 1 FROM #__forum_subscriptions WHERE user_id='.$yarbb_user['id'].' AND topic_id='.$topic_id);
	$database->query();
	if (!$database->getNumRows())
		mosRedirect(sefRelToAbs(YARBB_URL.'&amp;task=viewtopic&amp;id='.$topic_id), _Y_SUBSKADD2);

	$database->setQuery('DELETE FROM #__forum_subscriptions
	WHERE user_id='.$yarbb_user['id'].' 
	AND topic_id='.$topic_id);
	$database->query();

	mosRedirect(sefRelToAbs(YARBB_URL.'&amp;task=viewtopic&amp;id='.$topic_id), _Y_SUBSKADD3);
} else 
	mosRedirect(sefRelToAbs(YARBB_URL), _Y_404);

?>
